package com.zuimei.zuimei.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zuimei.zuimei.R;
import com.zuimei.zuimei.beans.CollectionsBean;

import java.util.List;

/**
 * Created by Jogger on 2017/2/3.
 * 用户收藏
 */

public class UserCollectionsAdapter extends RecyclerView.Adapter<UserCollectionsAdapter
        .MyViewHolder> {
    private Context mContext;
    private List<CollectionsBean> mCollectionsList;
    private OnItemClickListener mListener;

    public interface OnItemClickListener {
        void onItemClick(View view, CollectionsBean collections);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public UserCollectionsAdapter(Context context) {
        mContext = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.collect_item_layout, parent,
                false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final CollectionsBean collections = mCollectionsList.get(position);
        Picasso.with(mContext)
                .load(collections.getIcon())
                .config(Bitmap.Config.RGB_565)
                .into(holder.iv_icon);
        holder.tv_title.setText(collections.getTitle());
        holder.tv_sub_title.setText(collections.getSource_msg());
        holder.tv_flowers.setText(collections.getUp_times() + "");
        holder.tv_comments.setText(collections.getComment_times() + "");
        holder.ll_item_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener!=null){
                    mListener.onItemClick(view,collections);
                }
            }
        });

        GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) holder
                .ll_item_container.getLayoutParams();
        if (position % 2 != 0) {
            layoutParams.leftMargin = 5;
            layoutParams.rightMargin = 10;
        } else {
            layoutParams.leftMargin = 10;
        }
        layoutParams.bottomMargin = 5;
        holder.ll_item_container.setLayoutParams(layoutParams);
    }

    public void loadData(List<CollectionsBean> collectionsList) {
        mCollectionsList = collectionsList;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mCollectionsList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        LinearLayout ll_item_container;
        ImageView iv_icon;
        TextView tv_title;
        TextView tv_sub_title;
        TextView tv_flowers;
        TextView tv_comments;

        public MyViewHolder(View itemView) {
            super(itemView);
            ll_item_container = (LinearLayout) itemView.findViewById(R.id.ll_item_container);
            iv_icon = (ImageView) itemView.findViewById(R.id.iv_icon);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_sub_title = (TextView) itemView.findViewById(R.id.tv_sub_title);
            tv_flowers = (TextView) itemView.findViewById(R.id.tv_flowers);
            tv_comments = (TextView) itemView.findViewById(R.id.tv_comments);
        }
    }
}
